using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComUtils.MasterSetup.DS;
using PCSComProduction.ValueAddTransact.DS;
using PCSComUtils.Common;

namespace PCSComProduction.ValueAddTransact.BO
{
	public class LaborTimeBO //: ILaborTimeBO
	{
		public LaborTimeBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IObjectBO Members
		/// <summary>
		/// - Add new Labor Time Master to database and return new ID
		/// - Assign new LaborTimeMasterID for each Labor Time Detail in DataSet
		/// - Update detail DataSet
		/// </summary>
		/// <param name="pobjLaborMaster">Labor Time Master object</param>
		/// <param name="pdstDetailData">Labor Time Detail Data</param>
		/// <returns>New LaborTimeMasterID</returns>
		//[AutoComplete(true)]
		public int AddAndReturnID(object pobjLaborMaster, DataSet pdstDetailData)
		{
			try
			{
				//add the master first
				PRO_LaborTimeMasterDS objPRO_LaborTimeMasterDS = new PRO_LaborTimeMasterDS();
				int intLaborTimeMasterID = objPRO_LaborTimeMasterDS.AddAndReturnID(pobjLaborMaster);
				//update the id into the detail
				//update the dataset all of this id
				for (int i=0; i<pdstDetailData.Tables[0].Rows.Count;i++)
				{
					if (pdstDetailData.Tables[0].Rows[i].RowState != DataRowState.Deleted)
					{
						pdstDetailData.Tables[0].Rows[i][PRO_LaborTimeDetailTable.LABORTIMEMASTERID_FLD] = intLaborTimeMasterID;
					}
				}
				
				//update the detail
				PRO_LaborTimeDetailDS objPRO_LaborTimeDetailDS = new PRO_LaborTimeDetailDS();
				objPRO_LaborTimeDetailDS.UpdateDataSet(pdstDetailData);

				//Get the data back in order to refresh the grid for later edit and delete

				return intLaborTimeMasterID;
			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			
		}
		/// <summary>
		/// Get all Labor Time detail data of Labor Time Master
		/// </summary>
		/// <param name="pintLaborTimeMasterID">Labor Time Master ID</param>
		/// <returns>All Labor Time detail data of Labor Time Master</returns>
		//[AutoComplete(true)]
		public DataSet GetDetailData(int pintLaborTimeMasterID)
		{
			try
			{
				PRO_LaborTimeDetailDS objPRO_LaborTimeDetailDS = new PRO_LaborTimeDetailDS();
				return objPRO_LaborTimeDetailDS.GetDetailData(pintLaborTimeMasterID);
			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}

		}
		/// <summary>
		/// Get Labor Time Master object from ID
		/// </summary>
		//[AutoComplete(true)]
		public object GetLaborMasterObject(int pintLaborMasterID)
		{
			return null;
		}

		/// <summary>
		/// Get Master Location object from ID
		/// </summary>
		//[AutoComplete(true)]
		public object GetMasterLocationVO(int pintMasterLocationID)
		{
			try 
			{
				MST_MasterLocationDS objMST_MasterLocationDS = new MST_MasterLocationDS();
				return objMST_MasterLocationDS.GetObjectVO(pintMasterLocationID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}

		}

		//[AutoComplete(true)]
		public void UpdateDataSet(System.Data.DataSet dstData)
		{
			// TODO:  Add LaborTimeBO.UpdateDataSet implementation
		}

		//[AutoComplete(true)]
		public void Update(object pobjLaborMaster)
		{
			// TODO:  Add LaborTimeBO.Update implementation
		}

		/// <summary>
		/// 1. Delete all Labor Time Detail by LaborTimeMasterID
		/// 2. Delete Labor Time Master object by LaborTimeMasterID
		/// </summary>
		//[AutoComplete(true)]
		public void Delete(object pObjectVO)
		{
			try
			{
				//delete the Detail first
				PRO_LaborTimeMasterVO objPRO_LaborTimeMasterVO = (PRO_LaborTimeMasterVO)pObjectVO;
				PRO_LaborTimeDetailDS objPRO_LaborTimeDetailDS = new PRO_LaborTimeDetailDS();
				objPRO_LaborTimeDetailDS.DeleteByMasterID(objPRO_LaborTimeMasterVO.LaborTimeMasterID);

				//delete the master
				PRO_LaborTimeMasterDS objPRO_LaborTimeMasterDS = new PRO_LaborTimeMasterDS();
				objPRO_LaborTimeMasterDS.Delete(objPRO_LaborTimeMasterVO.LaborTimeMasterID);
			

			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
			// TODO:  Add LaborTimeBO.Add implementation
		}

		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add LaborTimeBO.GetObjectVO implementation
			return null;
		}


		/// <summary>
		/// Update the Labor time
		/// Update the master first
		/// and then update the detail data 
		/// </summary>
		/// <param name="pobjLaborMaster"></param>
		/// <param name="pdstDetailData"></param>
		//[AutoComplete(true)]
		public void UpdateLaborTime(object pobjLaborMaster, DataSet pdstDetailData)
		{
			try
			{
				//add the master first
				PRO_LaborTimeMasterDS objPRO_LaborTimeMasterDS = new PRO_LaborTimeMasterDS();
				objPRO_LaborTimeMasterDS.Update(pobjLaborMaster);
			
				//update the detail
				PRO_LaborTimeDetailDS objPRO_LaborTimeDetailDS = new PRO_LaborTimeDetailDS();
				objPRO_LaborTimeDetailDS.UpdateDataSet(pdstDetailData);
			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion
	}
}
